﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Bungalow.Spider.Elements
{

    public abstract class element
    {
        private SpiderView mHost;
        public element(SpiderView host)
        {
            this.mHost = host;
        }
        public void assignChildren()
        {
		    foreach(element c in getChildren())
		    {
			
			    if(c.getBounds() == null)
			    {
				    c.setBounds(new Rectangle(0,0,0,0));
			    }
			    if(c.getBounds().Width <= 0)
			    {
				    c.mBounds.Width = getWidth();
			    }
			    if(c.getBounds().Height <= 0)
			    {
				    c.mBounds.Height = getHeight();
			    }
		    }
	    }
        private int width;
        private int height;
        public int getWidth()
        {
            return this.width;
        }
        public void setWidth(int width)
        {
            this.width = width;
        }
        public int getHeight()
        {
            return this.height;
        }
        public void setHeight(int height)
        {
            this.height = height;
        }
        public void setBounds(Rectangle rectangle)
        {
            this.mBounds = rectangle;
        }
        private int padding;
        public int getPadding()
        {
            return padding;
        }
        public void setPadding(int padding)
        {
            this.padding = padding;
        }
        public void backendRender(params Object[] args) { }
        public Rectangle mBounds = new Rectangle();
        public Rectangle Bounds
        {
            get
            {
                return this.mBounds;
            }
        }
        public Rectangle getBounds()
        {
            return this.mBounds;
        }
        public void drawChildren(Graphics g, Rectangle bounds) {
            assignChildren();
		
		
		    foreach(element child in mChildren)
		    {
			    child.paint(g, new Rectangle(child.getBounds().X, child.getBounds().Y,child.getBounds().Width,child.getBounds().Height));
		    }
        }
        /***
	 * gets the child at
	 * @param point
	 * @return
	 */
        public element childAt(Point point)
	    {
		    assignChildren();
	
		
		    foreach(element child in mChildren)
		    {
			    if(point.X >= child.getBounds().X &&
				    point.X <= child.getBounds().X +child.getBounds().Width &&
				    point.X >= child.getBounds().Y &&
				    point.X <= child.getBounds().Y + child.getBounds().Height
					    )
			    {
				    element dchild = child.childAt(new Point(point.X+child.getBounds().X,point.Y+child.getBounds().Y));
				    if(dchild != null)
				    {
					    return dchild;
				    }
				    return child;
				
			    }
		    }
		    return null;
		
	    }
        public  void mouseOver(Point absolutePoints) { }
        public void mouseUp(Point absolutePoints) { }
        public void mouseDown(Point relativePoint) { }
        public void paint(Graphics g, Rectangle bounds)
        {
                
        }
        private List<element> mChildren = new List<element>();
        private int flex;
        public List<element> getChildren()
        {
            return this.mChildren;
        }
        public int getFlex()
        {
            return this.flex;
        }


        internal void onLoad(object[] p)
        {
            throw new NotImplementedException();
        }
    }
    
}
